package cn.v2tila.rsosa.student.service.serviceImpl;

import cn.v2tila.rsosa.student.entity.ZpxxEntity;
import cn.v2tila.rsosa.student.jpa.ZpxxJPA;
import cn.v2tila.rsosa.student.service.ZpxxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * @author v2tila on 14:26 2019/3/15
 */
@Service
public class ZpxxServiceImpl implements ZpxxService {
    @Autowired
    private ZpxxJPA zpxxJPA;


    public ZpxxEntity saveAndFlush(ZpxxEntity zpxxEntity) {
        return zpxxJPA.saveAndFlush(zpxxEntity);
    }

    public List<ZpxxEntity> findByStudentName(String studentName) {
        return zpxxJPA.findByStudentName(studentName);
    }
    public ZpxxEntity findById(Integer id) {
        Optional<ZpxxEntity> findResult = zpxxJPA.findById(id);
        return findResult.get();
    }

    @Override
    public Page<ZpxxEntity> findByCollegeName(String collegeName,int page) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page, 10, sort);
        List<String> colleges = new ArrayList<>();
        colleges.add(collegeName);
        colleges.add("全校级");
        Page<ZpxxEntity> pages = zpxxJPA.findByCollegeNameInAndCode(colleges, 1,pageable);
        return pages;
    }

    @Override
    public Page<ZpxxEntity> searchZpxx(int page, String college, String searchVal) {

        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page, 10, sort);
        List<String> colleges = new ArrayList<>();
        colleges.add(college);
        colleges.add("全校级");
        Page<ZpxxEntity> pages = zpxxJPA.findByAssoNameContainingAndCollegeNameInAndCode(searchVal,colleges, 1,pageable);

        return pages;
    }
}
